package com.youhui.weixin.controller;

import com.youhui.weixin.common.MythConfig;
import com.youhui.weixin.common.exception.MythException;
import com.youhui.weixin.service.ExcelRedisService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;

/**
 * Created by https://github.com/kuangcp on 17-10-7  下午2:09
 * 文件处理 Redis中配置信息的处理
 */
@Controller
public class FileController {

    @Autowired
    ExcelRedisService excelService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;
    private org.slf4j.Logger log = LoggerFactory.getLogger(FileController.class);

    @RequestMapping(value = "/UploadExcel", method = RequestMethod.POST)
    public ModelAndView testUploadFile(HttpServletRequest req,
                                       MultipartHttpServletRequest multiReq) {
        Long id = (Long) req.getSession().getAttribute("admin");
        ModelAndView view = new ModelAndView("admin/imports");
        // 获取上传文件的路径
//        String uploadFilePath = multiReq.getFile("file1").getOriginalFilename();
//        System.out.println("uploadFlePath:" + uploadFilePath);
        // 截取上传文件的文件名
//        String uploadFileName = uploadFilePath.substring(uploadFilePath.lastIndexOf('\\') + 1, uploadFilePath.indexOf('.'));
//        System.out.println("multiReq.getFile()" + uploadFileName);
        // 截取上传文件的后缀
//        String uploadFileSuffix = uploadFilePath.substring(uploadFilePath.indexOf('.') + 1, uploadFilePath.length());
//        System.out.println("uploadFileSuffix:" + uploadFileSuffix);

        FileInputStream fis = null;
        try {
            fis = (FileInputStream) multiReq.getFile("excelFile").getInputStream();
            excelService.readExcel(fis, id+"");
        } catch (MythException e) {
            e.printStackTrace();
            view.addObject("status","mapping");
            return view;
        } catch (IOException e) {
            e.printStackTrace();
            view.addObject("status","failed");
            return view;
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        view.addObject("status","SUCCESS");
        return view;
    }
    /**
     * 修改redis中关于Excel的定义配置
     */
    @PostMapping("/updateExcel")
    public String updateExcelDefine(HttpServletRequest request){

        Map map = request.getParameterMap();
        for(Object key:map.keySet()){
//            System.out.println(key.toString()+"^^^"+((String[])map.get(key))[0]);
            excelService.writeHashInRedis(MythConfig.EXCEL_READ_DEFINE, key.toString(), ((String[])map.get(key))[0].toUpperCase());
        }
        return "admin/config";

    }

    /**
     * 修改类型关键字匹配的
     */
    @PostMapping("/updateType")
    public String updateType(HttpServletRequest request){
        Map map = request.getParameterMap();
//        excelService.deleteHashKey(MythConfig.TYPE_DICT);
        for(Object key:map.keySet()){
            excelService.writeHashInRedis(MythConfig.TYPE_DICT, key.toString(), ((String[])map.get(key))[0]);
//            System.out.println(key.toString()+"^^^"+((String[])map.get(key))[0]);
        }
        return "admin/config";
    }
}
